package com.lingxu.base.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.lingxu.base.config.mybatis.IdModTableNameParser;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * 单数据源配置
 * @Author BaiDH
 *
 */
@Configuration
@MapperScan(value={"com.lingxu.**.mapper*"})
public class MybatisPlusConfig {

    /**
         *  分页插件
     */
//    @Bean
//    public PaginationInterceptor paginationInterceptor() {
//        // 设置sql的limit为无限制，默认是500
//        return new PaginationInterceptor().setLimit(-1);
//    }

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        HashMap<String, TableNameHandler> map = new HashMap<String, TableNameHandler>();
        //这里为不同的表设置对应表名处理器
//        map.put("user_daily_record", new DaysTableNameParser());
//        map.put("sjmf_case_time_axis_ql", new IdModTableNameParser(20));
        map.put("sjmf_case_billflow_excavate", new IdModTableNameParser(20));
        map.put("sjmf_case_pay_login_excavate", new IdModTableNameParser(20));
        map.put("sjmf_case_pay_flow_excavate", new IdModTableNameParser(20));
        dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map);
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);

        //添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
        //添加乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());

        return interceptor;
    }
   
}
